package dreadnought;
import javax.swing.*;
import java.awt.*;
/**
 * Represents the grid of the enemy which may be either
 * the computer in single player mode or a remote player
 * in online mode.
 * This grid also has 5 ships, but they are not visible to the
 * local player. The local player can click on cells to fire at them.
 * They local player can also see which cells have been fired at
 * and which shots hit an enemy ship and which shots hit water.
 */
public class EnemyGrid extends Grid
{

    public EnemyGrid(Game game)
    {
        //pass game reference to parent constructor
        super(game);

        //add ships to panel
        //this is only for testing
        //they will not be added to the panel in EnemyGrids
        //in the production version
        //add to layer 1 wich is above layer 0
        //GridCells are in layer 0 by default
        for (Ship s : this.ships)
        {
            //this.add(s, new Integer(1));
        }

        this.deployShips();
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
    }
    


    //this method is called by a GridCell when it is clicked
    //after a grid cell on an EnemyGrid is clicked, we must determine
    //whether or not this cell is occupied by a ship
    //if it is, we need to adjust the damage of the ship
    @Override
    public void fire(GridCoordinate coord)
    {
        super.fire(coord);
        
        GridCell cell = this.getCellFromCoordinate(coord);

        //try to get the ship at the coordinate of the cell
        //null is returned if no ship is at coordinate
        Ship ship = this.getShipAtCoordinate(coord);

        if (ship != null)
        {
            //handle a hit
            //determine the block id for the block of the ship which was hit
            int blockId = ship.getBlockId(coord);
            //inflict damage to the block which was hit
            ship.inflictDamage(blockId);

            //make the cell display itself as being a hit
            cell.markAsHit();

            //update the game status message
            game.writeToLog("player fire " + coord + " (hit)\n");

            //check to see if ship has been sunk
            //when a ship is sunk, a message box should be shown
            if (ship.isSunk())
            {
                String msg = "enemy's "
                        + ship.getType() + " has been sunk!\n";
                game.alert(msg);
                game.writeToLog(msg);
            }
            //check to see if all ships have been sunk
            if (allShipsSunk())
            {
                String msg = "All enemy ships have been sunk. player wins.";
                game.alert(msg);
                game.writeToLog("Gameover. Player is Victorious.\n");
                game.setState(GameState.GAMEOVER);
            }
            else
            {
                //the player hit a ship so he/she gets to keep playing
                game.setState(GameState.PLAYER_TURN);
            }
        }
        else
        {
            //handle a miss

            //update the game status message
            game.writeToLog("player fire " + coord + " (miss)\n");

            //make the cell display itself as being a miss
            cell.markAsMiss();

            game.setState(GameState.ENEMY_TURN);
        }

        game.turnEvent();
    }
}
